/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.channel.web.ChannelTemplateAction
   Module Description   :

   Date Created      : 2010/8/6
   Original Author   : feeling
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.channel.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.FastDateFormat;
import org.apache.commons.lang.xwork.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.channel.entity.ChannelTemplate;
import com.yaodian100.ec.channel.service.ChannelService;
import com.yaodian100.ec.common.EcConstant;

/**
 * @author feeling
 * 
 */
public class ChannelTemplateAction extends ActionSupport implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6534903290759245212L;
	private final Logger logger = LoggerFactory.getLogger(ChannelTemplateAction.class);
	@Autowired
	private ChannelService channelService;
	@Autowired
	private MenuService menuService;

	/* list */
	private Menu templateTypeMenu;
	private Menu channelStatusMenu;
	private Menu channelNameMenu;
	private Map<String, Object> channelStatusMap;

	/* search */
	private String templateTypeParam;
	private String endDateParam;
	private String channelNameParam;
	private String channelStatusParam;

	/* edit */
	private String id;
	private String channelName;
	private String templateType;
	private String contents;
	private String endDate;
	private String endTime;
	private String sTime;
	private String eTime;
	private String channelStatus;

	private ChannelTemplate template;
	private SimplePager pager;
	private List<ChannelTemplate> templateList;

	public String add() throws Exception {
		template = new ChannelTemplate();
		template.setId(null);
		template.setChannelName(null);
		template.setContents(null);
		template.setEndDate(null);
		template.setChannelStatus("Y");
		endTime = null;
		contents = null;
		id = null;
		return "edit";
	}

	public String edit() throws Exception {
		FastDateFormat dateFormat = FastDateFormat.getInstance("HH:mm");
		if (StringUtils.isNotBlank(id)) {
			template = channelService.getChannelTemplate(Long.parseLong(id));
			endTime = dateFormat.format(template.getEndDate());
			channelStatus = template.getChannelStatus();
			contents = template.getContents();
		}
		return "edit";
	}

	public String getChannelName() {
		return channelName;
	}

	public Menu getChannelNameMenu() {
		return channelNameMenu;
	}

	public String getChannelNameParam() {
		return channelNameParam;
	}

	public ChannelService getChannelService() {
		return channelService;
	}

	public String getChannelStatus() {
		return channelStatus;
	}

	public Map<String, Object> getChannelStatusMap() {
		return channelStatusMap;
	}

	public Menu getChannelStatusMenu() {
		return channelStatusMenu;
	}

	public String getChannelStatusParam() {
		return channelStatusParam;
	}

	public String getContents() {
		return contents;
	}

	public String getEndDate() {
		return endDate;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public String getEndTime() {
		return endTime;
	}

	public String geteTime() {
		return eTime;
	}

	public String getId() {
		return id;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public SimplePager getPager() {
		return pager;
	}

	public String getsTime() {
		return sTime;
	}

	public ChannelTemplate getTemplate() {
		return template;
	}

	public List<ChannelTemplate> getTemplateList() {
		return templateList;
	}

	public String getTemplateType() {
		return templateType;
	}

	public Menu getTemplateTypeMenu() {
		return templateTypeMenu;
	}

	public String getTemplateTypeParam() {
		return templateTypeParam;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (null == pager)
			pager = new SimplePager();
		pager.setPageRecord(20);// defaut 20

		if (null == channelNameMenu)
			channelNameMenu = menuService.get("Channel.Name");

		if (null == templateTypeMenu)
			templateTypeMenu = menuService.get("Channel.TemplateType");

		if (null == channelStatusMenu)
			channelStatusMenu = menuService.get("Channel.Status");

		channelStatusMap = new LinkedHashMap<String, Object>();
		channelStatusMap.put(channelStatusMenu.getOptions().get("Y").getCode(), channelStatusMenu.getOptions().get("Y")
				.getName());
		channelStatusMap.put(channelStatusMenu.getOptions().get("N").getCode(), channelStatusMenu.getOptions().get("N")
				.getName());

	}

	public String save() throws Exception {
		try {
			DateFormat format = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);
			Date eDate = null;
			if (StringUtils.isNotBlank(id))
				template = channelService.getChannelTemplate(Long.parseLong(id));
			else
				template = new ChannelTemplate();
			template.setType(templateType);
			template.setChannelName(channelName);
			template.setChannelStatus(channelStatus);
			template.setStartDate(new Date());
			template.setContents(contents);
			if (StringUtils.isNotBlank(endDate) && StringUtils.isNotBlank(endTime))
				eDate = format.parse(endDate + " " + endTime + ":59");

			template.setEndDate(eDate);
			channelService.saveChannelTemplate(template);
			addActionMessage("资料异动成功");
		} catch (Exception e) {
			addActionError("资料异动失败");
		}
		return "edit";
	}

	public String search() throws Exception {
		CommonCriteria criteria = new CommonCriteria();
		if (StringUtils.isNotBlank(channelNameParam))
			criteria.addEq("channelName", channelNameParam);
		if (StringUtils.isNotBlank(endDateParam))
			criteria.addLe("endDate", DateUtil.convertStringToDate(endDateParam + " 23:59:59"));
		if (StringUtils.isNotBlank(templateTypeParam))
			criteria.addEq("type", templateTypeParam);
		if (StringUtils.isNotBlank(channelStatusParam) && !StringUtils.equalsIgnoreCase("A", channelStatusParam))
			criteria.addEq("channelStatus", channelStatusParam);

		int templateSize = channelService.getChannelTemplateListSize(criteria);
		pager.setTotalSize(templateSize);
		templateList = channelService.getChannelTemplateListPageable(criteria, new String[] { "endDate asc" }, pager
				.getCurrentPage()
				* pager.getPageRecord(), pager.getPageRecord());
		return "list";
	}

	public void setChannelName(String channelName) {
		this.channelName = channelName;
	}

	public void setChannelNameMenu(Menu channelNameMenu) {
		this.channelNameMenu = channelNameMenu;
	}

	public void setChannelNameParam(String channelNameParam) {
		this.channelNameParam = channelNameParam;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	public void setChannelStatus(String channelStatus) {
		this.channelStatus = channelStatus;
	}

	public void setChannelStatusMap(Map<String, Object> channelStatusMap) {
		this.channelStatusMap = channelStatusMap;
	}

	public void setChannelStatusMenu(Menu channelStatusMenu) {
		this.channelStatusMenu = channelStatusMenu;
	}

	public void setChannelStatusParam(String channelStatusParam) {
		this.channelStatusParam = channelStatusParam;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void seteTime(String eTime) {
		this.eTime = eTime;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setsTime(String sTime) {
		this.sTime = sTime;
	}

	public void setTemplate(ChannelTemplate template) {
		this.template = template;
	}

	public void setTemplateList(List<ChannelTemplate> templateList) {
		this.templateList = templateList;
	}

	public void setTemplateType(String templateType) {
		this.templateType = templateType;
	}

	public void setTemplateTypeMenu(Menu templateTypeMenu) {
		this.templateTypeMenu = templateTypeMenu;
	}

	public void setTemplateTypeParam(String templateTypeParam) {
		this.templateTypeParam = templateTypeParam;
	}
}
